package fun.chutianshu.eescore_07_aop.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fun.chutianshu.eescore_07_aop.entity.UserInfo;
import fun.chutianshu.eescore_07_aop.mappers.UserInfoMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

// aop 切面类
@Aspect
@Component
// 响应顺序
@Order(1)
public class PermissionAdvice {

    @Autowired
    UserInfoMapper userInfoMapper;


    //切入点指向自定义的注解
    @Pointcut("@annotation(fun.chutianshu.eescore_07_aop.annotation.PermissionAnnotation)")
    private void permissionCheck() {
    }

    //切入处理，进行用户的权限验证
    @Around("permissionCheck()")
    public Object permissionCheck(ProceedingJoinPoint joinPoint) throws Throwable {

        //获取请求参数，详见接口类
        Object[] objects = joinPoint.getArgs();

        String token = ((JSONObject) objects[0]).getString("tonken");

        System.out.println("token： >>>>>>>>>>>>>>>>>>>>>>" + token);

        if (token == null || !token.equals("token!123")) {
            return JSON.parseObject("{\"message\":\"用户未登录，无权访问当前接口api！\",\"code\":403}");
        }
        else {
            System.out.println("用户验证通过，token = "+ token);
        }
        return joinPoint.proceed();
    }
}
